using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.Filtron;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Helpers.Translations;
using FiltronASP.Helpers.CatalogueHelpers;
using System.Net;

namespace FiltronASP.Helpers.LastSearches
{
    public class LastSearchFilter
    {
        public static string generateFilterDescription(int filterId, string filterProd, string languageName) {
            switch (filterProd) { 
                case "F":
                    return generateFiltronFilterDescription(filterId, languageName);
                case "W":
                    return generateWixFilterDescription(filterId, languageName);
            }
            return String.Empty;
        }

        private static string generateFiltronFilterDescription(int filterId, string languageName) {
            string htmlToReturn = "";
            IFiltronFilterDAO filtronFilterDAO = new FiltronFilterDAO();
            FiltronFilter filter = filtronFilterDAO.FindByPK(filterId);
            htmlToReturn += "<div class=\"searchRightSideTop\" id=\"searchRightSideTop\">";
            htmlToReturn += "                <div id=\"searchRightSideTopLeft\">";
            htmlToReturn += "                    <h3>" + translationHelper.getTranslation("_filter", languageName) + "  <span>" + FiltronHelper.GetFiltronFilterNumber(filter) + "</span></h3>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftDimensions\">";
            htmlToReturn += "                        <strong>" + translationHelper.getTranslation("_hlpSizes", languageName) + " (mm):</strong>";
            htmlToReturn += "                        <table>";
            string sizeG = String.Empty;
            string sizeF = String.Empty;
            if (filter.filter_dim_g_text != null) { sizeG = filter.filter_dim_g_text; } else { sizeG = Convert.ToString(SingleFilterHelper.parseFilterSizes(filter.filter_dim_g)); }
            if (filter.filter_dim_f_text != null) { sizeF = filter.filter_dim_f_text; } else { sizeF = Convert.ToString(SingleFilterHelper.parseFilterSizes(filter.filter_dim_f)); }

            htmlToReturn += "                            <tr><td>A: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_a) + "</span></td><td>B: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_b) + "</span></td><td>C: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_c) + "</span></td></tr>";
            htmlToReturn += "                            <tr><td>D: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_d) + "</span></td><td>E: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_e) + "</span></td><td>F: <span>" + sizeF + "</span></td></tr>";
            htmlToReturn += "                            <tr><td>G: <span>" + sizeG + "</span></td><td>H: <span>" + SingleFilterHelper.parseFilterSizes(filter.filter_dim_h) + "</span></td></tr>";
            htmlToReturn += "                        </table>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftInfo\">";
            htmlToReturn += "                        <ul>";

            string byPassValve = translationHelper.getTranslation("_hlpNo", languageName);
            string antiDrain = translationHelper.getTranslation("_hlpNo", languageName);
            string antiSyphon = translationHelper.getTranslation("_hlpNo", languageName);
            if (filter.filter_by_pass_valve == true)
            {
                byPassValve = translationHelper.getTranslation("_hlpYes", languageName);
            }
            if (filter.filter_anti_drain_valve == true)
            {
                antiDrain = translationHelper.getTranslation("_hlpYes", languageName);
            }
            if (filter.filter_anti_siphon_valve == true)
            {
                antiSyphon = translationHelper.getTranslation("_hlpYes", languageName);
            }

            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpByPassValve", languageName) + ": <span>{0}</span></li>", byPassValve);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpAntiDrain", languageName) + ": <span>{0}</span></li>", antiDrain);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpAntiSiphon", languageName) + ": <span>{0}</span></li>", antiSyphon);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpInstruction", languageName) + ": <span>{0}</span></li>", String.Empty);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpApplication", languageName) + ":<br/><span>{0}</span></li>", filter.filter_principle_applications);
            htmlToReturn += "                        </ul>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftCommands\">";
            htmlToReturn += "                        <img onclick=\"printFilterDescription()\" src=\"../../../Content/images/engine_printer.jpg\" alt=\"Printer\" /> <span onclick=\"printFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpPrint", languageName) + "</span>";
            htmlToReturn += "                        <img onclick=\"sendFilterDescription()\" src=\"../../../Content/images/engine_mail.jpg\" alt=\"Mail\" /> <span onclick=\"sendFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpSend", languageName) + "</span>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                </div>";
            htmlToReturn += "                <div id=\"searchRightSideTopRightFiltron\">";

            string imgPath = @"http://wixeurope.com/Content/images/filters/Filtron/" + filter.filter_wix_number_hd + ".jpg";
            string imgUrl = String.Empty;
            string imgUrlLarge = String.Empty;
            bool showZoom = true;
            var request = (HttpWebRequest)WebRequest.Create(imgPath);
            request.Method = "HEAD";
            try
            {
                var response = (HttpWebResponse)request.GetResponse();
                imgUrl = imgPath;
                imgUrlLarge = @"http://wixeurope.com/Content/images/filters/Filtron/large/" + FiltronHelper.GetFiltronFilterNumber(filter) + ".jpg";
            }
            catch
            {
                IFiltronFilterMediaDAO filtronFilterMediaDAO = new FiltronFilterMediaDAO();
                FiltronFilterMedia filterMedia = filtronFilterMediaDAO.FindByPK(filter.filter_media_id);
                string orgFilterMedia = String.Empty;
                if (filterMedia != null)
                {
                    orgFilterMedia = filterMedia.media_name;
                    string RepImageName = FiltronHelper.GetFiltronFilterImage(orgFilterMedia);
                    imgUrl = String.Format(@"http://wixeurope.com/Content/images/filters/Filtron/{0}.jpg", RepImageName);
                }
                showZoom = false;
            }

            htmlToReturn += "                    <img src=\""+ imgUrl +"\" alt=\"Example\" />";
            if (showZoom)
                htmlToReturn += "                    <a href=\""+ imgUrlLarge +"\" rel=\"lightbox\"><span>" + translationHelper.getTranslation("_hlpZoom", languageName) + " <img src=\"../../../Content/images/engine_magn.png\" alt=\"Zoom\" /></a></span>";
            htmlToReturn += "                </div>";
            htmlToReturn += "            </div>";
            return htmlToReturn;
        }

        private static string generateWixFilterDescription(int filterId, string languageName) {
            string htmlToReturn = "";

            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            WixFilter filter = wixFilterDAO.FindByPK(filterId);

            htmlToReturn += "<div class=\"searchRightSideTop\" id=\"searchRightSideTop\">";
            htmlToReturn += "                <div id=\"searchRightSideTopLeft\">";
            htmlToReturn += "                    <h3>" + translationHelper.getTranslation("_filter", languageName) + " <span>" + filter.filter_number + "</span></h3>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftDimensions\">";
            htmlToReturn += "                        <strong>" + translationHelper.getTranslation("_hlpSizes", languageName) + " (mm):</strong>";
            htmlToReturn += "                        <table>";
            if (filter.filter_outside_dia_top > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterTop", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_outside_dia_top)) + "</span></td></tr>";
            }
            else
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterTop", languageName) + ": <span> --- </span></td></tr>";
            }

            if (filter.filter_outside_dia_bottom > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterBottom", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_outside_dia_bottom)) + "</span></td></tr>";
            }
            else
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_outerDiamaterBottom", languageName) + ": <span> --- </span></td></tr>";
            }
            if (filter.filter_inside_dia_top > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_innerDiameter", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_inside_dia_top)) + "</span></td></tr>";
            }
            else
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_innerDiameter", languageName) + ": <span> --- </span></td></tr>";
            }
            if (filter.filter_height > 0)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_height", languageName) + ": <span>" + SingleFilterHelper.parseFilterSizes(SingleFilterHelper.convertInchesToMetric(filter.filter_height)) + "</span></td></tr>";
            }
            else
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_height", languageName) + ": <span> --- </span></td></tr>";
            }
            if (filter.filter_thread_size != null)
            {
                htmlToReturn += "                            <tr><td style=\"width: 290px;\">" + translationHelper.getTranslation("_threadSize", languageName) + ": <span>" + filter.filter_thread_size + "</span></td></tr>";
            }
            htmlToReturn += "                        </table>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftInfo\">";
            htmlToReturn += "                        <ul>";

            string filterStyle = "---";
            string filterType = "---";
            string filterMedia = "---";
            string filterService = "---";
            if ((filter.filter_style_id > 0) && (filter.filter_style_id != null))
            {
                filterStyle = SingleFilterHelper.getFilterStyle(Convert.ToInt32(filter.filter_style_id));
            }
            if ((filter.filter_type_id > 0) && (filter.filter_type_id != null))
            {
                filterType = SingleFilterHelper.getFilterType(Convert.ToInt32(filter.filter_type_id));
            }
            if ((filter.filter_media_id > 0) && (filter.filter_media_id != null))
            {
                filterMedia = SingleFilterHelper.getMediaType(Convert.ToInt32(filter.filter_media_id));
            }
            if ((filter.filter_service_id > 0) && (filter.filter_service_id != null))
            {
                filterMedia = SingleFilterHelper.getFilterService(Convert.ToInt32(filter.filter_service_id));
            }

            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterStyle", languageName) + ": <span>{0}</span></li>", filterStyle);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterType", languageName) + ": <span>{0}</span></li>", filterType);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterMedia", languageName) + ": <span>{0}</span></li>", filterMedia);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpFilterService", languageName) + ": <span>{0}</span></li>", filterService);

            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpInstruction", languageName) + ": <span>{0}</span></li>", String.Empty);
            htmlToReturn += String.Format("<li>" + translationHelper.getTranslation("_hlpApplication", languageName) + ":<br/><span>{0}</span></li>", filter.filter_principle_applications);
            htmlToReturn += "                        </ul>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                    <div class=\"searchRightSideTopLeftCommands\">";
            htmlToReturn += "                        <img onclick=\"printFilterDescription()\" src=\"../../../Content/images/engine_printer.jpg\" alt=\"Printer\" /> <span onclick=\"printFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpPrint", languageName) + "</span>";
            htmlToReturn += "                        <img onclick=\"sendFilterDescription()\" src=\"../../../Content/images/engine_mail.jpg\" alt=\"Mail\" /> <span onclick=\"sendFilterDescription()\" class=\"desc_commands\">" + translationHelper.getTranslation("_hlpSend", languageName) + "</span>";
            htmlToReturn += "                    </div>";
            htmlToReturn += "                </div>";
            htmlToReturn += "                <div id=\"searchRightSideTopRight\">";
            htmlToReturn += "                    <img src=\"../../../Content/images/temp/example_filter.png\" alt=\"Example\" />";
            htmlToReturn += "                    <a href=\"../../../Content/images/temp/example_filter.png\" rel=\"lightbox\"><span>" + translationHelper.getTranslation("_hlpZoom", languageName) + " <img src=\"../../../Content/images/engine_magn.png\" alt=\"Zoom\" /></a></span>";
            htmlToReturn += "                </div>";
            htmlToReturn += "            </div>";

            return htmlToReturn;
        }
    }
}
